home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Utilities / FinderV3 / Source / FinderV3Main.c < prev    next >
C/C++ Source or Header  |  1997-07-03  |  14KB  |  548 lines

  1. /* Main-Header File inserted by GenCodeC */
  2. /* Libraries */
  3. #include <libraries/mui.h>
  4. #include <libraries/gadtools.h> /* for BARLABEL in MenuItem */
  5.  
  6. /* Prototypes */
  7. #include <clib/muimaster_protos.h>
  8. #include <clib/exec_protos.h>
  9. #include <clib/alib_protos.h>
  10. #include <clib/dos_protos.h>
  11. #include <clib/asl_protos.h>
  12.  
  13. /*  Pragmas  */
  14. #include <pragmas/muimaster_pragmas.h>
  15. #include <pragmas/asl_pragmas.h>
  16.  
  17. /* Increase stack size */
  18. LONG __stack=8192;
  19. /* GenCodeC header end */
  20.  
  21. /* Include generated by GenCodeC */
  22. #include "FinderV3GUI.c"
  23.  
  24. /* Misc Includes */
  25.  
  26. /* Declarations for libraries (inserted by GenCodeC) */
  27. struct Library * IntuitionBase;
  28. struct Library * MUIMasterBase;
  29.  
  30. struct Library ASLBase;
  31.  
  32. /* Global declarations */
  33.  
  34. BOOL debug = FALSE;
  35.  
  36. LONG active;
  37. struct ObjApp * App = NULL;    /* Object */
  38. char *drawer_str, *pattern_str, search_str[256];
  39. #define MAXFILELEN 256
  40. char *finished = "\033bOperation successful";
  41. char search_banner[256], num_banner[256];
  42. int files_found = 0;
  43.  
  44. /* Search options */
  45. BOOL *recurse_bool = FALSE, *case_bool = FALSE;
  46.  
  47. /* ASL declarations */
  48. struct TagItem frtags[] = 
  49.    {
  50.    ASL_Hail,      (ULONG)"Choose file to save list to...",
  51.    ASL_Height,    400,
  52.    ASL_Width,     320,
  53.    ASL_LeftEdge,  0,
  54.    ASL_TopEdge,   0,
  55.    ASL_OKText,    (ULONG)"Ok",
  56.    ASL_CancelText,(ULONG)"Cancel",
  57.    ASL_File,      (ULONG)"Finder.list",
  58.    ASL_Dir,       (ULONG)"RAM:",
  59.    TAG_DONE
  60.    };
  61.  
  62. /* Function prototypes */
  63. void GUIFind(void);
  64. BOOL SearchDir(char *directory, char *pattern);
  65. void Save_List(void);
  66.  
  67. /* Init() function */
  68. void init( void )
  69. {
  70.     if (!(IntuitionBase = OpenLibrary("intuition.library",37)))
  71.     {
  72.         printf("Can't Open Intuition Library\n");
  73.         exit(20);
  74.     }
  75.     if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
  76.     {
  77.         printf("Can't Open MUIMaster Library\n");
  78.         CloseLibrary(IntuitionBase);
  79.         exit(20);
  80.     }
  81. }
  82. /* GenCodeC init() end */
  83.  
  84. /* End() function */
  85. void end( void )
  86. {
  87.     CloseLibrary(MUIMasterBase);
  88.     CloseLibrary(IntuitionBase);
  89.     exit(0);
  90. }
  91. /* GenCodeC end() end */
  92.  
  93. /*
  94. ** App message callback hook. Note that the object given here
  95. ** is the object that called the hook, i.e. the one that got
  96. ** the icon(s) dropped on it.
  97. */
  98.  
  99. SAVEDS ASM LONG AppMsgFunc(REG(a2) APTR obj, REG(a1) struct AppMessage **x)
  100. {
  101. struct WBArg *ap;
  102. struct AppMessage *amsg = *x;
  103. int i;
  104. static char buf[256];
  105. char *b = buf;
  106.  
  107. for ( ap = amsg->am_ArgList, i = 0 ; i < amsg->am_NumArgs ; i++, ap++ )
  108.    {
  109.  
  110.    NameFromLock(ap->wa_Lock, buf, sizeof(buf));
  111.    AddPart(buf, ap->wa_Name, sizeof(buf));
  112.  
  113.    /* We need to strip away down to the trailing / if this is a file */
  114.    do {
  115.       if (buf[strlen(buf) - 1] != '/')
  116.          {
  117.          buf[strlen(buf) - 1] = '\0';
  118.          }
  119.       } while (buf[strlen(buf) - 1] != '/');
  120.  
  121.    }
  122.  
  123. set(App->STR_DrawerPopup, MUIA_String_Contents, b);
  124.  
  125. return(0);
  126. }
  127.  
  128. /* Main Function inserted by GenCodeC */
  129. int main(int argc, char *argv[])
  130. {
  131.  
  132.     BOOL    running = TRUE;
  133.     ULONG    signal;
  134.    static const struct Hook AppMsgHook = { { NULL,NULL },(VOID *)AppMsgFunc,NULL,NULL };
  135.  
  136.     /* Program initialisation : generated by GenCodeC */
  137.     init();
  138.  
  139.     /* Create Object : generated by GenCodeC */
  140.     if (!(App = CreateApp()))
  141.     {
  142.         printf("Can't Create App\n");
  143.         end();
  144.     }
  145.  
  146.    /* App hook stuff */
  147.    DoMethod(App->STR_DrawerPopup, MUIM_Notify, MUIA_AppMessage, MUIV_EveryTime,
  148.       App->STR_DrawerPopup, 3, MUIM_CallHook, &AppMsgHook, MUIV_TriggerValue);
  149.  
  150.    set(App->App, MUIA_Application_DropObject, App->STR_DrawerPopup);
  151.  
  152.     while (running)
  153.     {
  154.         switch (DoMethod(App->App, MUIM_Application_Input, &signal))
  155.         {
  156.             case MUIV_Application_ReturnID_Quit:
  157.             get(App->DrawerPopup, MUIA_Popasl_Active, &active);
  158.             if (active)
  159.                MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Please close the ASL File Requester\nbefore exiting");
  160.             else
  161.                    running = FALSE;
  162.            break;
  163.  
  164.             /* Insert your code between the "case" statement and comment "end of case ..." */
  165.  
  166.          case FindPressed:
  167.             GUIFind();
  168.          break;
  169.          /* end of case Find */
  170.  
  171.             case SaveList:
  172.             Save_List();
  173.          break;
  174.             /* end of case SaveList */
  175.  
  176.             case PrintList:
  177.             MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Sorry, this feature has not been implemented yet");
  178.          break;
  179.             /* end of case PrintList */
  180.  
  181.             /* End computing of IDCMP */
  182.  
  183.             default:
  184.            break;
  185.         }
  186.         if (running && signal) Wait(signal);
  187.     }
  188.     DisposeApp(App);
  189.     end();
  190. }
  191.  
  192. /*
  193. ** void GUIFind()
  194. **
  195. ** This function sorts out actually finding (wow!)
  196. **
  197. */
  198. void GUIFind()
  199. {
  200.  
  201. char *ptr = search_banner;
  202. char *num_ptr = num_banner;
  203.  
  204. /* Error buffer */
  205. LONG error_buf;
  206.  
  207. /* Clear the current list*/
  208. DoMethod(App->FileList, MUIM_List_Clear);
  209. files_found = 0;
  210.  
  211. /* Put the application to sleep while we find */
  212. set(App->MainWin, MUIA_Window_Sleep, TRUE);
  213.  
  214. /* Get the contents of the MUI string objects and search options*/
  215. get(App->STR_DrawerPopup, MUIA_String_Contents, &drawer_str);
  216. get(App->PatternStr, MUIA_String_Contents, &pattern_str);
  217. get(App->casecheck, MUIA_Selected, &case_bool);
  218. get(App->recursecheck, MUIA_Selected, &recurse_bool);
  219.  
  220. if (debug)
  221.    {
  222.    if (case_bool != FALSE)
  223.       printf("Case sensetive\n");
  224.  
  225.    if (recurse_bool)
  226.       printf("Recursive\n");
  227.  
  228.    printf("Debug1: Dir: %s, Pattern %s\n", drawer_str, pattern_str);
  229.    }
  230.  
  231. if (strlen(drawer_str) == 0 || strlen(pattern_str) == 0)
  232.    {
  233.    MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Error: You \033bmust\033n supply a drawer and a pattern");
  234.  
  235.    set(App->MainWin, MUIA_Window_Sleep, FALSE);
  236.  
  237.    return;
  238.    }
  239.  
  240. /* Prepare our search */
  241. if (case_bool)
  242.    error_buf = ParsePattern(pattern_str, search_str, MAXFILELEN);
  243. else
  244.    error_buf = ParsePatternNoCase(pattern_str, search_str, MAXFILELEN);
  245.  
  246.    if (error_buf == -1)
  247.       {
  248.       /* The Parse has failed */
  249.       MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Error: A serious internal error has occured,\npossibly a buffer overflow");
  250.       set(App->MainWin, MUIA_Window_Sleep, FALSE);
  251.       return;
  252.       }
  253.  
  254. /* Put up a banner saying what we are doing */
  255. sprintf(search_banner, "\033bFiles matching '%s' in '%s'", pattern_str, drawer_str);
  256.  
  257. /*DoMethod(App->FileList, MUIM_List_Insert, &ptr, 1, MUIV_List_Insert_Bottom);*/
  258. set(App->FileList, MUIA_List_Title, ptr);
  259.  
  260. /* Call the searcher function */
  261. if (!(SearchDir(drawer_str, search_str)))
  262.    {
  263.    MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Error: Search failed.");
  264.    set(App->MainWin, MUIA_Window_Sleep, FALSE);
  265.  
  266.    return;
  267.    }
  268.  
  269. /* Put up the file counter */
  270. sprintf(num_banner, "-- %i files found", files_found);
  271. DoMethod(App->FileList, MUIM_List_Insert, &num_ptr, 1, MUIV_List_Insert_Bottom);
  272.  
  273. /* Search has finished, put up the completion banner */
  274. DoMethod(App->FileList, MUIM_List_Insert, &finished, 1, MUIV_List_Insert_Bottom);
  275. set(App->FileList, MUIA_List_Active, MUIV_List_Active_Bottom);
  276.  
  277. /* Unbusy the window */
  278. set(App->MainWin, MUIA_Window_Sleep, FALSE);
  279.  
  280. return;
  281.  
  282. }
  283.  
  284. /*
  285. ** BOOL SearchDir(char *directory, char *pattern)
  286. **
  287. ** This is the function which actually accesses the disk to find the files
  288. **  and compares their names with the pattern supplied.
  289. **
  290. */
  291. BOOL SearchDir(char *directory, char *pattern)
  292. {
  293.  
  294. __aligned struct FileInfoBlock   fib;
  295.  
  296. BPTR lk = NULL;
  297. char full_path[255];
  298.  
  299. char *fpath = full_path;
  300.  
  301. /* Lock initial directory and store the FileInfoBlock */
  302. if (!(lk = Lock(directory, ACCESS_READ)))
  303.    {
  304.    MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Error: Unable to lock directory");
  305.    if (lk) UnLock(lk);
  306.    return FALSE;
  307.    }
  308. if (!(Examine(lk, &fib)))
  309.    {
  310.    MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Error: Unable to access FileInfoBlock");
  311.    if (lk) UnLock(lk);
  312.    return FALSE;
  313.    }
  314.  
  315. /* Scan directory */
  316. while (ExNext(lk, &fib))
  317.    {
  318.    if (SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
  319.       {
  320.       MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Ctrl-C received, stopping search");
  321.       UnLock(lk);
  322.       return FALSE;
  323.       }
  324.  
  325.    /* Build the full path with our ExNext file */
  326.    strcpy(full_path, directory);
  327.    AddPart(full_path, fib.fib_FileName, 255);
  328.  
  329.    /* Identify this file (is it a directory?) */
  330.    if (fib.fib_DirEntryType > 0)
  331.       {
  332.       /* We have a directory, recursively scan it (if we want to)  */
  333.       if (recurse_bool)
  334.          {
  335.          if (!(SearchDir(full_path, pattern)))
  336.             {
  337.             UnLock(lk);
  338.             return FALSE;
  339.             }
  340.          }
  341.       }
  342.    else
  343.       {
  344.       /* We have a file, match it with our pattern */
  345.       if (!case_bool)
  346.          {
  347.          if (MatchPatternNoCase(pattern, fib.fib_FileName))
  348.             {
  349.             if (debug) printf("We have a match on file '%s'\n", fib.fib_FileName);
  350.             DoMethod(App->FileList, MUIM_List_Insert, &fpath, 1, MUIV_List_Insert_Bottom);
  351.             set(App->FileList, MUIA_List_Active, MUIV_List_Active_Bottom);
  352.             files_found++;
  353.             }
  354.          }
  355.       if (case_bool)
  356.          {
  357.          if (MatchPattern(pattern, fib.fib_FileName))
  358.             {
  359.             if (debug) printf("We have a match on file '%s'\n", fib.fib_FileName);
  360.             DoMethod(App->FileList, MUIM_List_Insert, &fpath, 1, MUIV_List_Insert_Bottom);
  361.             set(App->FileList, MUIA_List_Active, MUIV_List_Active_Bottom);
  362.             files_found++;
  363.             }
  364.          }
  365.       }
  366.    }
  367.  
  368.    UnLock(lk);
  369.  
  370. return TRUE;
  371.  
  372. }
  373.  
  374. /*
  375. ** void Save_List(void)
  376. **
  377. ** This function will save the list to a file specified by the user
  378. **
  379. */
  380. void Save_List( void )
  381. {
  382.  
  383. /* Variable definitions for the list saving loop */
  384. int i;
  385. FILE *fp = NULL;
  386. LONG result;
  387. char fr_drawer[256];
  388.  
  389. struct FileRequester *fr;
  390.  
  391. /* Make sure we actually have a list to save */
  392. if (!drawer_str)
  393.    {
  394.    MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Error: There is no list to save");
  395.  
  396.    return;
  397.    }
  398.  
  399. /* Let the user choose a file */
  400.  
  401. if (!(fr = (struct FileRequester *)MUI_AllocAslRequest(ASL_FileRequest, frtags)))
  402.    {
  403.    MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Error: Unable to allocate the ASL requester");
  404.    if (fr) MUI_FreeAslRequest(fr);
  405.  
  406.    return;
  407.    }
  408.  
  409. if (!(MUI_AslRequest(fr, NULL)))
  410.    {
  411.    MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Error: User cancelled\n\033cList saving aborted");
  412.    if (fr) MUI_FreeAslRequest(fr);
  413.  
  414.    return;
  415.    }
  416.  
  417. /* Copy the tags to a AddPart type storage thingy */
  418. strcpy(fr_drawer, fr->rf_Dir);
  419. AddPart(fr_drawer, fr->rf_File, 255);
  420.  
  421. MUI_FreeAslRequest(fr);
  422.  
  423. /* Kludge zone!! (almost) */
  424. /* We need to know if the file exists so we know whether or not to pop up an append/overwrite requester */
  425. /* This is done by trying to open the file we want with read flags */
  426. /*  If the file exists, this will fill the FILE pointer */
  427. /*  we can test for this. If the pointer is not NULL, the file exists and we pop up our requester */
  428. /*  Simple! */
  429.  
  430. if (!(fp = fopen("RAM:Finder.list", "r")))
  431.    {
  432.       /* The open has failed, therefore, the file doesn't exist and we can just get on and write to it */
  433.       /* Now open the file new for writing */
  434.       if (fp) fclose(fp); /* Close the FILE pointer, just in case */
  435.  
  436.       if (!(fp = fopen("RAM:Finder.list", "w")))
  437.          {
  438.             MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Error: Unable to open the file");
  439.             if (fp) fclose(fp);
  440.             return;
  441.          }
  442.  
  443.       /* Loop to read the list contents into the file */
  444.       /* First, put the window to sleep */
  445.       set(App->MainWin, MUIA_Window_Sleep, TRUE);
  446.  
  447.       fprintf(fp, "Search results for drawer '%s' with pattern '%s'\n", drawer_str, pattern_str);
  448.  
  449.       for ( i = 0 ;; i++ )
  450.          {
  451.             char *name;
  452.  
  453.             DoMethod(App->FileList, MUIM_List_GetEntry, i , &name);
  454.             if (!name)
  455.                break;
  456.  
  457.             fprintf(fp, "%s\n", name);
  458.          }
  459.  
  460.       fclose(fp);
  461.       set(App->MainWin, MUIA_Window_Sleep, FALSE);
  462.  
  463.       return;
  464.  
  465.    }
  466. else
  467.    {
  468.       fclose(fp);
  469.       /* The file exists, we now need to decide if we want to overwrite, or append it */
  470.  
  471.       /* Append/Overwrite requester - return values:              2        1        0 */
  472.       result = MUI_Request(App->App, App->MainWin, 0, NULL, "_Append|_Overwrite|_Cancel", "The file already exists, please choose an operation");
  473.  
  474.       if (result == 2)
  475.          {
  476.             /* The user wants to overwrite the file */
  477.  
  478.             /* Open the file for overwriting */
  479.             if (!(fp = fopen("RAM:Finder.list", "w")))
  480.                {
  481.                   MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Error: Unable to open the file");
  482.                   if (fp) fclose(fp);
  483.  
  484.                   return;
  485.                }
  486.  
  487.             /* List loop */
  488.             set(App->MainWin, MUIA_Window_Sleep, TRUE);
  489.  
  490.             fprintf(fp, "Search results for drawer '%s' with pattern '%s'\n", drawer_str, pattern_str);
  491.  
  492.             for ( i = 0 ;; i++ )
  493.                {
  494.                   char *name;
  495.  
  496.                   DoMethod(App->FileList, MUIM_List_GetEntry, i , &name);
  497.                   if (!name)
  498.                      break;
  499.  
  500.                   fprintf(fp, "%s\n", name);
  501.                }
  502.  
  503.             fclose(fp);
  504.             set(App->MainWin, MUIA_Window_Sleep, FALSE);
  505.  
  506.             return;
  507.  
  508.          }
  509.    if (result == 1)
  510.          {
  511.             /* The user wants to append the file */
  512.  
  513.             /* Open the file for appending */
  514.             if (!(fp = fopen("RAM:Finder.list", "a")))
  515.                {
  516.                   MUI_Request(App->App, App->MainWin, 0, NULL,"Ok","Error: Unable to open the file");
  517.                   if (fp) fclose(fp);
  518.  
  519.                   return;
  520.                }
  521.  
  522.             /* List loop */
  523.             set(App->MainWin, MUIA_Window_Sleep, TRUE);
  524.  
  525.             fprintf(fp, "\nSearch results for drawer '%s' with pattern '%s'\n", drawer_str, pattern_str);
  526.  
  527.             for ( i = 0 ;; i++ )
  528.                {
  529.                   char *name;
  530.  
  531.                   DoMethod(App->FileList, MUIM_List_GetEntry, i , &name);
  532.                   if (!name)
  533.                      break;
  534.  
  535.                   fprintf(fp, "%s\n", name);
  536.                }
  537.  
  538.             fclose(fp);
  539.             set(App->MainWin, MUIA_Window_Sleep, FALSE);
  540.  
  541.             return;
  542.             }
  543.  
  544.          }
  545.  
  546. return;
  547.  
  548. }